package main

import (
	"fmt"
	"go-web-learning/src/04_gorm/db"
)

var DB = db.DB

func main() {
	//initData()
	//var students []db.Student
	//// 查询用户名是'小王'的
	//DB.Where("name = ?", "小王").Find(&students)
	//fmt.Println(students)
	//
	//// 查询用户名不是'小王'的
	//DB.Not("name = ?", "小王").Find(&students)
	//fmt.Println(students)
	//
	//// 查询用户名包含'老'的用户
	//DB.Where("name like ?", "%老%").Find(&students)
	//fmt.Println(students)
	//
	//// 查询用户'小王'和'老李'
	//DB.Where("name in ?", []string{"小王", "老李"}).Find(&students)
	//fmt.Println(students)
	//
	//// 查询年龄大于23，且是女生的
	//DB.Where("age > ?", 23).Where("gender = ?", "女").Find(&students)
	//fmt.Println(students)
	//
	//// 查询年龄不大于25,或者是女生的
	//DB.Where("age <= ?", 25).Or("gender = ?", "女").Find(&students)
	//fmt.Println(students)
	//
	//// 使用结构体查询
	//DB.Where(&db.Student{Age: 25, Name: "小王"}).Find(&students)
	//fmt.Println(students)
	//
	//// 使用map查询
	//DB.Where(map[string]any{"name": "小王", "age": 26}).Find(&students)
	//fmt.Println(students)
	//
	//// select选择字段
	//DB.Select("name", "age").Find(&students)
	//// DB.Select([]string{"name", "age"}).Find(&students)
	//fmt.Println(students)
	//
	//// scan
	//fmt.Println("--------------------------")
	//var studentBaseInfos []db.StudentBaseInfo
	////DB.Table("students").Select("name", "age").Scan(&studentBaseInfo)
	//DB.Model(&db.Student{}).Select("name", "age").Scan(&studentBaseInfos)
	//fmt.Println(studentBaseInfos)

	//// 排序
	//DB.Order("age asc").Find(&[]db.Student{}).Scan(&studentBaseInfos)
	//fmt.Println(studentBaseInfos)
	//
	//// 分页查询
	//// 第一页
	//DB.Limit(4).Offset(0).Find(&[]db.Student{}).Scan(&studentBaseInfos)
	//fmt.Println(studentBaseInfos)
	//// 第二页
	//DB.Limit(4).Offset(4).Find(&[]db.Student{}).Scan(&studentBaseInfos)
	//fmt.Println(studentBaseInfos)
	//
	//// 通用分页
	//limit := 4
	//page := 1
	//DB.Limit(limit).Offset((page - 1) * limit).Find(&[]db.Student{}).Scan(&studentBaseInfos)
	//fmt.Println(studentBaseInfos)

	//// 去重
	//var ageList []int
	////DB.Model(db.Student{}).Select("age").Distinct("age").Scan(&ageList)
	//DB.Model(db.Student{}).Select("distinct age").Scan(&ageList)
	//fmt.Println(ageList)

	//// 分组查询
	//var countList []db.GenderCount
	//DB.Model(db.Student{}).
	//	Select("GROUP_CONCAT(name) as name",
	//		" count(id) as count",
	//		" gender",
	//	).
	//	Group("gender").
	//	Scan(&countList)
	//fmt.Println(countList)

	// 执行原生sql
	var countList []db.GenderCount
	DB.Raw("SELECT GROUP_CONCAT(name) as name, COUNT(id) as count, gender FROM students GROUP BY gender;").Scan(&countList)
	fmt.Println(countList)
}

func initData() {
	DB.Create([]db.Student{
		{Name: "小王", Age: 25, Gender: "男", Email: ptrString("xiaowang@163.com")},
		{Name: "小赵", Age: 23, Gender: "女", Email: ptrString("xiaozhao@163.com")},
		{Name: "老李", Age: 26, Gender: "男", Email: ptrString("xiaoli@163.com")},
		{Name: "小孙", Age: 21, Gender: "女", Email: ptrString("xiaosun@163.com")},
		{Name: "小高", Age: 28, Gender: "男", Email: ptrString("xiaogao@163.com")},
		{Name: "老张", Age: 26, Gender: "女", Email: ptrString("xiaozhang@163.com")},
	})
}

func ptrString(s string) *string {
	return &s
}
